package net.lab1024.sa.admin.module.business.erp.mmo.service;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.lab1024.sa.admin.module.business.erp.inv.dao.InventordDao;
import net.lab1024.sa.admin.module.business.erp.mmo.dao.PwrordDao;
import net.lab1024.sa.admin.module.business.erp.mmo.dao.PwrordaDao;
import net.lab1024.sa.admin.module.business.erp.inv.domain.entity.InventordEntity;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.entity.PwrordEntity;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.entity.PwrordaEntity;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.form.PwrordAddForm;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.form.PwrordQueryForm;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.form.PwrordUpdateForm;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.vo.PwrordVO;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.vo.PwrordaVO;
import net.lab1024.sa.base.common.code.UserErrorCode;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 产品入库单 Service
 *
 * @Author AKITA
 * @Date 2025-08-18 22:49:00
 * @Copyright AKITA
 */

@Service
public class PwrordService {

    @Resource
    private PwrordDao pwrordDao;
    @Resource
    private PwrordaDao pwrordaDao;
    @Resource
    private InventordDao inventordDao;

    /**
     * 分页查询
     */
    public PageResult<PwrordVO> queryPage(PwrordQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<PwrordVO> list = pwrordDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    @Transactional
    public ResponseDTO<Long> add(PwrordAddForm addForm) {
        PwrordEntity pwrordEntity = SmartBeanUtil.copy(addForm, PwrordEntity.class);
        pwrordDao.insert(pwrordEntity);

        InventordEntity inventordEntity = new InventordEntity();
        for (PwrordaEntity pwrordaEntity : addForm.getLista()) {
            pwrordaEntity.setId(pwrordEntity.getId());
            inventordEntity.setItemId(pwrordaEntity.getItemId());
            inventordEntity.setItemName(pwrordaEntity.getItemName());
            inventordEntity.setBatchId(pwrordaEntity.getBatchId());
            inventordEntity.setStoId(pwrordaEntity.getStoId());
            inventordEntity.setStoName(pwrordaEntity.getStoName());
            inventordEntity.setQty(pwrordaEntity.getQty());
            inventordEntity.setSpecs(pwrordaEntity.getSpecs());
            inventordEntity.setUnit(pwrordaEntity.getUnit());
            inventordEntity.setQuality(pwrordaEntity.getQuality());
            inventordEntity.setId(null);
            inventordEntity.setBaseId(pwrordaEntity.getId());
            inventordEntity.setBaseLineNum(pwrordaEntity.getLineNum());
            inventordDao.insert(inventordEntity);
            pwrordaDao.insert(pwrordaEntity);
        }
        return ResponseDTO.ok(pwrordEntity.getId());
    }

    /**
     * 更新
     *
     */
    public ResponseDTO<String> update(PwrordUpdateForm updateForm) {
        PwrordEntity pwrordEntity = SmartBeanUtil.copy(updateForm, PwrordEntity.class);
        pwrordDao.updateById(pwrordEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        pwrordDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    @Transactional
    public ResponseDTO<String> delete(Long id) {

        List<PwrordaEntity> pwrordaEntities = pwrordaDao.selectList(new LambdaQueryWrapper<PwrordaEntity>().eq(PwrordaEntity::getId, id));

        for (PwrordaEntity pwrordaEntity : pwrordaEntities) {
            InventordEntity inventordEntity = inventordDao.selectOne(new LambdaQueryWrapper<InventordEntity>().eq(InventordEntity::getBatchId, pwrordaEntity.getBatchId()));
            if (inventordEntity.getQty().compareTo(pwrordaEntity.getQty()) == 0) {
                inventordDao.deleteById(inventordEntity.getId());
            } else {
                return ResponseDTO.error(UserErrorCode.PARAM_ERROR, "物料已经被消耗，禁止删除！");
            }
        }




        pwrordDao.deleteById(id);
        pwrordaDao.deleteById(id);
        return ResponseDTO.ok();
    }

    public ResponseDTO<PwrordVO> info(Long id) {
        PwrordEntity pwrordEntity = pwrordDao.selectById(id);
        PwrordVO copy = SmartBeanUtil.copy(pwrordEntity, PwrordVO.class);
        List<PwrordaEntity> pwrordaEntities = pwrordaDao.selectList(new LambdaQueryWrapper<PwrordaEntity>().eq(PwrordaEntity::getId, id));
        List<PwrordaVO> pwrordaVOS = SmartBeanUtil.copyList(pwrordaEntities, PwrordaVO.class);
        copy.setLista(pwrordaVOS);
        return ResponseDTO.ok(copy);

    }

    public ResponseDTO<PwrordVO> infoUnexecute(Long id) {
        List<PwrordVO> poordVOS = pwrordDao.infoUnexecute(id);
        return ResponseDTO.ok(poordVOS.get(0));



    }
}
